package com.genITeam.ria.dao;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.hibernate.Session;

import org.hibernate.criterion.Expression;

import com.genITeam.ria.exception.NGFException;
import com.genITeam.ria.utility.HibernateUtils;
import com.genITeam.ria.vo.Buddy;
import com.genITeam.ria.vo.Member;
import com.genITeam.ria.vo.ResponseVo;

public class MemberDAO {

	public MemberDAO() {
		BasicConfigurator.configure();
	}

	Logger logger = Logger.getLogger(MemberDAO.class);

	/**
	 * createMember Used to create a new Member
	 * 
	 * @param Member,
	 *            Session
	 * 
	 * @return none
	 * @throws NGFException
	 */

	public void createMember(Member memberVo, Session session)
			throws NGFException {

		try {
			logger.info("Start createMember");
			session.saveOrUpdate(memberVo);

		} catch (Exception e) {
			throw new NGFException(e.getMessage());
		}
	}

	/**
	 * isUserExist this method is use to check the existense of user name
	 * 
	 * @param Member,
	 *            Session
	 * 
	 * @return ResponseVo
	 * @throws NGFException
	 */

	public ResponseVo isUserExist(Member memberVo, Session session)
			throws NGFException {
		ResponseVo responseVo = new ResponseVo();
		responseVo.setMessage("success");

		try {
			logger.info("Start isUserExist");
			Member member = new Member();
			member = (Member) session.createCriteria(Member.class).add(
					Expression.eq("userName", memberVo.getUserName()))
					.uniqueResult();

			if (member != null) {
				responseVo
						.setMessage("Some one has already taken this User Name please enter the User Name again");
				return responseVo;
			}

		} catch (Exception e) {
			System.out.print("Message" + e.getMessage());
			logger.error(e.toString());
			throw new NGFException(e.getMessage());
			// logger.error(e.toString());

		}
		return responseVo;
	}

	/**
	 * addBuddy this method is use to add a new buddy
	 * 
	 * @param Member,
	 *            Session
	 * 
	 * @return ResponseVo
	 * @throws NGFException
	 */

	public void addBuddy(Buddy buddy, Session session) throws NGFException {

		try {
			logger.info("Start addBuddy");
			System.out.println("Inside Add buddy DAO");

			session.saveOrUpdate(buddy);

			System.out.println("Updated");

		} catch (Exception e) {
			logger.error(e.toString());
			throw new NGFException(e.getMessage());
		}

	}

	/**
	 * getmemberData this method is use to add a new buddy
	 * 
	 * @param int,
	 *            Session
	 * 
	 * @return Member
	 * @throws NGFException
	 */

	public Member getmemberData(String id, Session session) throws NGFException {

		Member member = new Member();
		try {
			logger.info("Start getmemberData");
			member = (Member) session.createCriteria(Member.class).add(
					Expression.eq("id", new Integer(id))).uniqueResult();

		} catch (Exception e) {
			System.out.println(e.toString());
			logger.error(e.toString());
			throw new NGFException(e.getMessage());
		}

		return member;
	}

	/**
	 * banUser this method is use to add a new buddy
	 * 
	 * @param Member,
	 *            Session
	 * 
	 * @return none
	 * @throws NGFException
	 */

	public void banUser(Member memberVo, Session session) throws NGFException {
		try {
			logger.info("Start banUser in Member DAO");
			session.saveOrUpdate(memberVo);

		} catch (Exception e) {
			logger.error(e.toString());
			throw new NGFException(e.getMessage());

		}

	}

	public static void main(String a[]) {
		Session session = null;
		try {
			session = HibernateUtils.getSessionFactory().openSession();

			Member memberVo = new Member();
			memberVo.setUserName("adnan");
			MemberDAO memberDAO = new MemberDAO();
			memberDAO.isUserExist(memberVo, session);
			session.close();

		} catch (Exception e) {
			
			System.out.println(e.toString());
			session.close();
		}
	}

}
